﻿
CREATE PROCEDURE [dbo].[s_Title_Fetch]
AS
SET NOCOUNT ON
SET IMPLICIT_TRANSACTIONS OFF
DECLARE cur CURSOR
READ_ONLY
FOR SELECT     ci.LoanID, ci.VIN
FROM   t_CarInfo ci INNER JOIN t_Loan l ON ci.LoanID = l.ID
WHERE    l.TitleOk IS NULL AND  (dbo.VinCheck(ci.VIN) = 1) AND (LEN(ci.VIN) = 17)
ORDER BY ci.LoanID

DECLARE @LoanID int,@VIN varchar(17)
DECLARE @Value int,@HTML nvarchar(max),@LHOT nvarchar(4000)
OPEN cur

FETCH NEXT FROM cur INTO @LoanID,@VIN
WHILE @@fetch_status = 0 BEGIN

	EXEC @Value = [dbo].[TitleInfo] 'localhost', @VIN,'AUTOBANK', @HTML OUTPUT,@LHOT OUTPUT

	PRINT CONVERT(varchar,@Value)+':'+@LHOT

	UPDATE t_Loan SET TitleOk = @Value,LienHolderOnTitle = @LHOT,TitleHTML = @HTML WHERE ID = @LoanID

	IF dbo.Stop() = 1 BREAK

	FETCH NEXT FROM cur INTO @LoanID,@VIN
END

CLOSE cur
DEALLOCATE cur

